layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    form.verify({
        rescId : function(val){
            //这里有一个约定，下拉会出现请选择，请选择的value=-1 所以后台的数据中字典对应的值不能设置为-1
            if(val == '-1'){
                return "分类必选";
            }
        }
    })


    form.on("submit(addMgrRescmenu)",function(data){
        //弹出loading
        var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.ajax({
            type: "post",
            url:"add",
            dataType: "json",
            data: $("#submitForm").serialize(),
            success: function (result) {
                if(result.flag == "success"){
                    layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();

                }else{
                    layer.msg("添加失败", {icon: 2});
                }
            }, error: function () {
                layer.msg("添加失败", {icon: 2});
            }

        });
    })

    form.on("submit(updateMgrRescmenu)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        // 实际使用时的提交信息
        $.ajax({
            type: "post",
            url:"update",
            dataType: "json",
            data: $("#submitForm").serialize(),
            success: function (result) {
                if(result.flag == "success"){
                    top.layer.close(index);
                    top.layer.msg("更新成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }else{
                    layer.msg("更新失败", {icon: 2});
                }
            }, error: function () {
                layer.msg("更新失败", {icon: 2});
            }
        });
    })

    //关闭
    $(".close").on("click",function(){
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    });

})